from django.db.migrations import serializer
from django.shortcuts import render
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.status import *

from drfstudy.models import Student, Classes
from drfstudy.serializers import StudentSerializer, ClassesSerializer


# Create your views here.
# class StudentViewSet(viewsets.ModelViewSet):
#     queryset = Student.objects.all()
#     serializer_class = StudentSerializer
#
#     # 获取一组数据
#     def list(self, request):
#         students = Student.objects.filter(is_delete=False)
#         # 把查询集转换成json格式
#         # 将查询集传入instance参数中，如果是多条
#         serializer = StudentSerializer(students, many=True)
#         print(serializer.data)
#         return Response(serializer.data)
#
#     def create(self, request):
#         serializer = StudentSerializer(data=request.data)
#         serializer.is_valid(raise_exception=True)
#         serializer.save()
#         return Response(serializer.data)
#
#     def retrieve(self, request, pk):
#         try:
#             student = Student.objects.filter(is_delete=False).get(id=pk)
#         except Student.DoesNotExist:
#             return Response(status=HTTP_404_NOT_FOUND)
#         serializer = StudentSerializer(student)
#         return Response(serializer.data)
#
#     def update(self, request, pk):
#         try:
#             student = Student.objects.get(id=pk)
#         except Student.DoesNotExist:
#             return Response(status=HTTP_404_NOT_FOUND)
#         # 当在为序列化器传入了两个参数时此时DRF自动判断是更新还是创建数据
#         serializer = StudentSerializer(student, data=request.data)
#         serializer.is_valid(raise_exception=True)
#         serializer.save()
#         return Response(serializer.data)
#
#     def destory(self, request, pk):
#         # 判断id是否存在
#         try:
#             student = Student.objects.get(id=pk)
#         except Student.DoesNotExist:
#             return Response(status=HTTP_404_NOT_FOUND)
#         student.delete()
#         return Response(status=HTTP_204_NO_CONTENT)
class StudentViewSet(viewsets.ModelViewSet):
    lookup_field = 'id'
    queryset = Student.objects.filter(is_delete=False)
    # 定义使用的序列化器
    serializer_class = StudentSerializer

    def list(self, request, *args, **kwargs):
        student = Student.objects.filter(is_delete=False)
        print(student)  # 每次列表请求打印
        return super().list(request, *args, **kwargs)


class ClassesViewSet(viewsets.ModelViewSet):
    # 显示的数据
    queryset = Classes.objects.filter(is_delete=False)
    # 定义使用的序列化器
    serializer_class = ClassesSerializer

    # 定义一个新的视图方法->返回最新创建的班级
    # api设计,GET请求 , classes/last/  detail = False 是否为详情路由，不是就不会存在pk参数
    @action(methods=['get'], detail=False)
    def last(self, request):
        classes = Classes.objects.last()
        print(classes)
        data = self.get_serializer(classes).data
        return Response(data)

    # api设计,GET/ /classes/<pk>/last_student/
    @action(methods=['get'], detail=True,url_path='last')
    def last_student(self, request, pk):
        try:
            classes = Classes.objects.get(id=pk)
        except Classes.DoesNotExist:
            return Response(status=HTTP_404_NOT_FOUND)
        classes_student_list = classes.student_set.last()
        data = StudentSerializer(classes_student_list).data
        return Response(data)